package com.fenghongzhang.day016;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private SeekBar seekBar;
    private  MusicService.MyBinder binder;
    private MusicService musicService;
    private Button model;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        model = (Button) findViewById(R.id.model);



        lv = (ListView) findViewById(R.id.lv);
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);
        }


        //拖动进度条
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    musicService.setProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

//        new MyAdapter()
//        lv.setAdapter();
        //启动服务-一直活着
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MyBinder) service;
                 musicService = binder.getService();

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,connection,BIND_AUTO_CREATE);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                binder.callPlay(position);
                musicService.play(position);
                if(timer == null){
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            //从服务取
                            seekBar.setMax(musicService.getMax());
                            seekBar.setProgress(musicService.getCurrPosition());
                        }
                    },1000,2000);
                }
            }
        });

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            // 数据源
            List<Music> data = MusicUtil.getData(this);
            MyAdapter myAdapter = new MyAdapter(data, this);
            lv.setAdapter(myAdapter);
        }else{

        }
    }

    public void pre(View view) {
        musicService.pre();
    }

    public void next(View view) {
        musicService.next();
    }

    public void restart(View view) {
        musicService.restart();
    }

    public void stop(View view) {
        musicService.stop();
    }

    public void pause(View view) {
//        binder.callPlay();
//        binder.callPause();
        musicService.pause();
    }

    public void model(View view) {
        final PopupWindow popupWindow = new PopupWindow();

        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(inflate);
        TextView order = inflate.findViewById(R.id.pop_order);
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "顺序", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                musicService.setPlayModel(MusicUtil.ORDER);
            }
        });

        TextView random = inflate.findViewById(R.id.pop_random);
        random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "随机", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
//                playModel = MusicUtil.RANDOM;
                musicService.setPlayModel(MusicUtil.RANDOM);
            }
        });

        TextView single = inflate.findViewById(R.id.pop_single);
        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "单曲", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
//                playModel = MusicUtil.SINGLE;
                musicService.setPlayModel(MusicUtil.SINGLE);
            }
        });


        popupWindow.setOutsideTouchable(true);

        popupWindow.showAsDropDown(model,0,-350);
    }
}
